module executor.search.server.SearchUserExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import hunt.util.Serialize;
import client.MsgServerClient;
import client.ImClient;
import client.LogicServerClient;
import std.array;
import std.conv : to;
import core.stdc.time;
import core.stdc.string;

@TransportServer(gateServerName)
class SearchUserExecutor : AbstractExecutor!(SearchUserExecutor) {

  @MessageId(MESSAGE.CMD_SEARCH_USER_REQ)
  void searchUser(TransportContext ctx, MessageBuffer buffer) {
    if(buffer is null)
    {
        logError("searchUser  buffer is null !!!!!");
        return ;
    }
    auto req = new SearchUserREQ();
    buffer.data.fromProtobuf!SearchUserREQ(req);

    User u = cast(User)ctx.session.getAttribute(SESSIONKEY.USER);
    AuthenticateREQ authorize = cast(AuthenticateREQ)ctx.session.getAttribute(SESSIONKEY.SRC);
    if(u !is null)
    {
        req.src = authorize.src;
    }

    MessageTransportClient client = LogicServerClient.getInstance.getForwardClient();

    ClientExtend clientExtend ;// = cast(ClientExtend)buffer.extend;
    memcpy(&clientExtend,buffer.extend.ptr,ClientExtend.sizeof);
    ServerExtend serverExtend;
    serverExtend.requestId = clientExtend.requestId;

    ubyte[ServerExtend.sizeof] extend ;
    memcpy(extend.ptr,&serverExtend,ServerExtend.sizeof);

    client.send(new MessageBuffer(MESSAGE.CMD_SEARCH_USER_REQ,req.toProtobuf.array,extend));
  }

  @MessageId(MESSAGE.CMD_SEARCH_BY_IDS_REQ)
  void searchByIds(TransportContext ctx, MessageBuffer buffer) {
    if(buffer is null)
    {
      logError("searchByIds  buffer is null !!!!!");
      return ;
    }
    auto req = new SearchByIdsREQ();
    buffer.data.fromProtobuf!SearchByIdsREQ(req);

    User u = cast(User)ctx.session.getAttribute(SESSIONKEY.USER);
    AuthenticateREQ authorize = cast(AuthenticateREQ)ctx.session.getAttribute(SESSIONKEY.SRC);
    if(u !is null)
    {
      req.src = authorize.src;
    }

    MessageTransportClient client = LogicServerClient.getInstance.getForwardClient();

    ClientExtend clientExtend ;// = cast(ClientExtend)buffer.extend;
    memcpy(&clientExtend,buffer.extend.ptr,ClientExtend.sizeof);
    ServerExtend serverExtend;
    serverExtend.requestId = clientExtend.requestId;

    ubyte[ServerExtend.sizeof] extend ;
    memcpy(extend.ptr,&serverExtend,ServerExtend.sizeof);
    client.send(new MessageBuffer(MESSAGE.CMD_SEARCH_BY_IDS_REQ,req.toProtobuf.array,extend));
  }

  @MessageId(MESSAGE.CMD_SEARCH_BY_ID_REQ)
  void searchById(TransportContext ctx, MessageBuffer buffer) {
    if(buffer is null)
    {
      logError("searchById  buffer is null !!!!!");
      return ;
    }
    auto req = new SearchByIdREQ();
    buffer.data.fromProtobuf!SearchByIdREQ(req);

    User u = cast(User)ctx.session.getAttribute(SESSIONKEY.USER);
    AuthenticateREQ authorize = cast(AuthenticateREQ)ctx.session.getAttribute(SESSIONKEY.SRC);
    if(u !is null)
    {
      req.src = authorize.src;
    }

    MessageTransportClient client = LogicServerClient.getInstance.getForwardClient();

    ClientExtend clientExtend ;//= cast(ClientExtend)buffer.extend;
    memcpy(&clientExtend,buffer.extend.ptr,ClientExtend.sizeof);
    ServerExtend serverExtend;
    serverExtend.requestId = clientExtend.requestId;

    ubyte[ServerExtend.sizeof] extend ;
    memcpy(extend.ptr,&serverExtend,ServerExtend.sizeof);

    client.send(new MessageBuffer(MESSAGE.CMD_SEARCH_BY_ID_REQ,req.toProtobuf.array,extend));
  }



  @MessageId(MESSAGE.CMD_GET_UER_BY_QRCODE_REQ)
  void getUserByQrCode(TransportContext ctx, MessageBuffer buffer) {
    auto req = new GetUserByQrCodeREQ();
    buffer.data.fromProtobuf!GetUserByQrCodeREQ(req);

    User u = cast(User)ctx.session.getAttribute(SESSIONKEY.USER);
    AuthenticateREQ authorize = cast(AuthenticateREQ)ctx.session.getAttribute(SESSIONKEY.SRC);
    if(u !is null)
    {
      req.src = authorize.src;
    }

    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_GET_UER_BY_QRCODE_REQ,req.toProtobuf.array));
    logInfof("getUserByQrCode  %s ---- %s",req.code, req.userId);
  }

}

